package by.grsu.hdm.devices;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import by.grsu.hdm.media.MediaPlayer;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport,
		INetworkAvailable {

	private long trafficIn;
	private long trafficOut;

	private UsbPort usbPort;
	private Map<String, MediaPlayer> mediaPlayers;


	protected MediaCenter() {
		defaultConfiguration();
	}

	public MediaCenter(String id) {
		super(id);
		defaultConfiguration();
	}

	protected void defaultConfiguration() {
		this.trafficIn = 0;
		this.trafficOut = 0;
		this.usbPort = null;
		this.mediaPlayers = new HashMap<String, MediaPlayer>();
	}

	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}

	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
		System.out.println(getDeviceId() + "is switched on");
	}

	@Override
	protected void beforeTurningOff() {
		defaultConfiguration();
		System.out.println(getDeviceId() + "is switched off");
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) {
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}
		return this.usbPort;
	}

	public void addMediaPlayer(String key, MediaPlayer value) {
		if (mediaPlayers != null) {
			mediaPlayers.put(key, value);
		}
	}

	public MediaPlayer getMediaPlayer(String key) {
		MediaPlayer result = null;
		if (mediaPlayers != null) {
			result = mediaPlayers.get(key);
		}
		return result;
	}

}
